package io.gitee.chearnee.fw.sidecar.client;

import io.gitee.chearnee.fw.sidecar.model.ApplicationRes;
import io.gitee.chearnee.fw.sidecar.model.BaseApplication;
import io.gitee.chearnee.fw.sidecar.util.ResultUtil;
import io.gitee.chearnee.fw.sidecar.workshop.ActionProduce;
import lombok.RequiredArgsConstructor;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author cn
 * @version 1.0
 * @description
 * @date 12/19/2022 3:47 PM
 */
@RequiredArgsConstructor
public class ClientHelper {

    private static String podNamespace;

    static {
        Map<String, String> env = System.getenv();
        if (env != null) {
            podNamespace = env.get("MY_POD_NAMESPACE");
        }
    }

    private final PlatClient platClient;

    public void process(Object[] objects) {
        if (objects == null || objects.length == 0) {
            return;
        }
        if (!StringUtils.hasText(podNamespace)) {
            return;
        }
        if (platClient == null) {
            return;
        }
        ResultUtil.getResultVo(platClient.action(objects), false);
    }

    public List<ApplicationRes> registerApplications(BaseApplication baseClientReq) {
        if (!StringUtils.hasText(podNamespace)) {
            return Collections.emptyList();
        }
        ActionProduce.init(baseClientReq);
        if (platClient == null) {
            return Collections.emptyList();
        }
        return ResultUtil.getResultVo(platClient.registerApplication(baseClientReq), false);
    }
}
